<?php


class Articles extends CActiveRecord
{
	 const STATUS_ACTIVE = 1;
    const STATUS_PENDING = 2;
    const STATUS_REJECTED = 0;
        
        public $Vvamjdmvic5i;
        public $Vwfk133vtore;
        public $Vif53wwzmbl1;
        public $Vizo3j5bossh;
        public $Vbfjw3di3qie;
        public $Vvzhqgqf4t4p;
        public $V0b1ihlordvk;
        public $Var5itcrymel;
        public $Vrwxpiewut2t;
        public $V54a4mp5olw3;
        public $Vjule3ystjlm;
        public $Vwg0zzlimxkj;
        public $Vlmv1jwbdio0;
        public $Vl4ul3ugim3b;
        public $Vtafxfbizgvb;
        public $Vpu0sywyhduk;
        public $Vp0ht3ph2ayz;
        public $Vllfux5zg3zw;
        public $Vexzyyshqy31;
        public $V3cb53weuyqt;      
        public $Vhi05miuvgcl;
        public $Veggaarzpo5h;
        public $Vklfzb2evkkt;
        public $Vbtjta1prh3h;
        public $Vok5egdbefwc;
        public $Vs1c0m51tg0o;
        public $Vpnbkjyse0lr;
	
	public function tableName()
	{
		return 'articles';
	}

	
	public function rules()
	{
		
		
		return array(
			array('code_name, display_name', 'required'),
			array('view_count, like_count, dislike_count, comment_count, favorite_count, duration, image_count, status, content_provider_id, using_duration', 'numerical', 'integerOnly'=>true),
			array('code_name, display_name, display_name_ascii', 'length', 'max'=>200),
			array('original_title, actors, director', 'length', 'max'=>1000),
			array('tags, short_description', 'length', 'max'=>500),
			array('create_user_id, modify_user_id', 'length', 'max'=>11),
			array('release_date, description, content, expiry_date, create_date, modify_date, approve_date', 'safe'),
			
			
			array('id, code_name, display_name, original_title, tags, actors, director, release_date, short_description, description, content, view_count, like_count, dislike_count, comment_count, favorite_count, duration, image_count, expiry_date, status, create_date, modify_date, create_user_id, modify_user_id, content_provider_id, using_duration, approve_date, display_name_ascii', 'safe', 'on'=>'search'),
		);
	}

	
	public function relations()
	{
		
		
		return array(
			'createUser' => array(self::BELONGS_TO, 'BackendUser', 'create_user_id'),
			'modifyUser' => array(self::BELONGS_TO, 'BackendUser', 'modify_user_id'),
			'vodImages' => array(self::HAS_MANY, 'VodImage', 'articles_id'),
			'articlesCategoryMappings' => array(self::HAS_MANY, 'ArticlesCategoryMapping', 'articles_id'),
			'articlesCategories'=>array(self::HAS_MANY,'articlesCategory','articles_category_id', 'through'=>'articlesCategoryMappings'),
		);
	}

	
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'code_name' => 'Code Name',
			'display_name' => 'Display Name',
			'original_title' => 'Original Title',
			'tags' => 'Tags',
			'actors' => 'Actors',
			'director' => 'Director',
			'release_date' => 'Release Date',
			'short_description' => 'Short Description',
			'description' => 'Description',
			'content' => 'Content',
			'view_count' => 'View Count',
			'like_count' => 'Like Count',
			'dislike_count' => 'Dislike Count',
			'comment_count' => 'Comment Count',
			'favorite_count' => 'Favorite Count',
			'duration' => 'Duration',
			'image_count' => 'Image Count',
			'expiry_date' => 'Expiry Date',
			'status' => 'Status',
			'create_date' => 'Create Date',
			'modify_date' => 'Modify Date',
			'create_user_id' => 'Create User',
			'modify_user_id' => 'Modify User',
			'content_provider_id' => 'Content Provider',
			'using_duration' => 'Using Duration',
			'approve_date' => 'Approve Date',
			'display_name_ascii' => 'Display Name Ascii',
		);
	}

	
	public function search()
	{
		

		$V0v5vsnfxezw=new CDbCriteria;

		$V0v5vsnfxezw->compare('id',$this->id);
		$V0v5vsnfxezw->compare('code_name',$this->code_name,true);
		$V0v5vsnfxezw->compare('display_name',$this->display_name,true);
		$V0v5vsnfxezw->compare('original_title',$this->original_title,true);
		$V0v5vsnfxezw->compare('tags',$this->tags,true);
		$V0v5vsnfxezw->compare('actors',$this->actors,true);
		$V0v5vsnfxezw->compare('director',$this->director,true);
		$V0v5vsnfxezw->compare('release_date',$this->release_date,true);
		$V0v5vsnfxezw->compare('short_description',$this->short_description,true);
		$V0v5vsnfxezw->compare('description',$this->description,true);
		$V0v5vsnfxezw->compare('content',$this->content,true);
		$V0v5vsnfxezw->compare('view_count',$this->view_count);
		$V0v5vsnfxezw->compare('like_count',$this->like_count);
		$V0v5vsnfxezw->compare('dislike_count',$this->dislike_count);
		$V0v5vsnfxezw->compare('comment_count',$this->comment_count);
		$V0v5vsnfxezw->compare('favorite_count',$this->favorite_count);
		$V0v5vsnfxezw->compare('duration',$this->duration);
		$V0v5vsnfxezw->compare('image_count',$this->image_count);
		$V0v5vsnfxezw->compare('expiry_date',$this->expiry_date,true);
		$V0v5vsnfxezw->compare('status',$this->status);
		$V0v5vsnfxezw->compare('create_date',$this->create_date,true);
		$V0v5vsnfxezw->compare('modify_date',$this->modify_date,true);
		$V0v5vsnfxezw->compare('create_user_id',$this->create_user_id,true);
		$V0v5vsnfxezw->compare('modify_user_id',$this->modify_user_id,true);
		$V0v5vsnfxezw->compare('content_provider_id',$this->content_provider_id);
		$V0v5vsnfxezw->compare('using_duration',$this->using_duration);
		$V0v5vsnfxezw->compare('approve_date',$this->approve_date,true);
		$V0v5vsnfxezw->compare('display_name_ascii',$this->display_name_ascii,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$V0v5vsnfxezw,
		));
	}

	
	public static function model($Vgmajjibziuy=__CLASS__)
	{
		return parent::model($Vgmajjibziuy);
	}
	public static function findArticles($Vceaol1c1qxm, $Vtkshvgl1ivp, $Veggaarzpo5h_no, $Veggaarzpo5h_size, $Vmq5toeun05t, $V5as2qkxgfs4 = -1, $Vrlyvhau3qpi = 1) {
    	$V24noxecbnll = "1";
        if($V5as2qkxgfs4 != -1) {
            $V24noxecbnll = "id = ".$V5as2qkxgfs4;
        }
        else {
        	if($Vrlyvhau3qpi > -1) {
	            $V24noxecbnll .= " AND (status = ".$Vrlyvhau3qpi.")"; 
        	}
            if (!empty ($Vmq5toeun05t)) {
                $V24noxecbnll .= " AND (t.display_name LIKE '%$Vmq5toeun05t%' OR t.display_name_ascii LIKE '%$Vmq5toeun05t%') ";
            }

            if ($Vceaol1c1qxm != null) {
                $Vfcisoqdzruw = ArticlesCategory::model()->findByPk($Vceaol1c1qxm)->path;
                $V24noxecbnll .= " AND id in (select articles_id from  articles_category_mapping where articles_category_id in (".
                           "select id from articles_category where concat('/',path,'/') like '/$Vfcisoqdzruw/%')) ";
            }
        }
        

        
        
        $Vxun03s52uac = Yii::app()->db->createCommand()
                ->selectDistinct('t.id,t.create_date,t.view_count,t.short_description,t.description,t.like_count,t.dislike_count,t.comment_count,t.favorite_count,t.display_name,t.duration,t.status,t.approve_date')
                ->from('articles as t')
                ->where($V24noxecbnll) 
                ->limit($Veggaarzpo5h_size, $Veggaarzpo5h_no * $Veggaarzpo5h_size)
                ->order($Vtkshvgl1ivp)
                ->queryAll();
        $Vfx3dng1y1f3 = Yii::app()->db->createCommand()
                ->select('count(id)')
                ->from('articles as t')
                ->where($V24noxecbnll) 
                ->queryScalar();
        
        
        
        
        
        return array(
            'total_result'=>$Vfx3dng1y1f3,
            'keyword'=>$Vmq5toeun05t,
            'page_number'=>$Veggaarzpo5h_no,
            'page_size'=>$Veggaarzpo5h_size,
            'category'=>$Vceaol1c1qxm,
            'total_page'=>($Vfx3dng1y1f3 - ($Vfx3dng1y1f3 % $Veggaarzpo5h_size))/$Veggaarzpo5h_size + (($Vfx3dng1y1f3 % $Veggaarzpo5h_size ===0)?0:1),
            'data'=>$Vxun03s52uac
        );
    }
    
    
    public static function findArticle1s($Vvamjdmvic5i, $Vtkshvgl1ivp, $Veggaarzpo5h_no, $Veggaarzpo5h_size, $Vmq5toeun05t, $V5as2qkxgfs4 = -1, $Vrlyvhau3qpi = 1) {
    	$V24noxecbnll = "1";
        if($V5as2qkxgfs4 != -1) {
            $V24noxecbnll = "id = ".$V5as2qkxgfs4;
        }
        else {
        	if($Vrlyvhau3qpi > -1) {
	            $V24noxecbnll .= " AND (status = ".$Vrlyvhau3qpi.")"; 
        	} 
            if (!empty ($Vmq5toeun05t)) {
                $V24noxecbnll .= " AND (t.display_name LIKE '%$Vmq5toeun05t%' OR t.display_name_ascii LIKE '%$Vmq5toeun05t%') ";
            }

            $V0ed0x4gides = "";
            foreach($Vvamjdmvic5i as $Vceaol1c1qxm) {
            	$Vde2xppeyzpl = ArticlesCategory::model()->findByPk($Vceaol1c1qxm);
            	if($Vde2xppeyzpl != NULL) {
	                $Vfcisoqdzruw = $Vde2xppeyzpl->path;
	                $V0ed0x4gides .= "(concat('/',path,'/') like '/$Vfcisoqdzruw/%') or ";
            	}
            }
            $Vexyoomomnpj = strlen($V0ed0x4gides)- 4;
            $V0ed0x4gides = substr($V0ed0x4gides, 0, $Vexyoomomnpj);
            $V24noxecbnll .= " AND id in (select articles_id from  articles_category_mapping where articles_category_id in (".
                           "select id from articles_category where ".$V0ed0x4gides."))";
        }
        
        $Vxun03s52uac = Yii::app()->db->createCommand()
                ->selectDistinct('t.id,t.create_date,t.view_count,t.short_description,t.description,t.like_count,t.dislike_count,t.comment_count,t.favorite_count,t.display_name,t.duration, t.status,t.approve_date')
                ->from('articles as t')
                ->where($V24noxecbnll) 
                ->limit($Veggaarzpo5h_size, $Veggaarzpo5h_no * $Veggaarzpo5h_size)
                ->order($Vtkshvgl1ivp)
                ->queryAll();
        $Vfx3dng1y1f3 = Yii::app()->db->createCommand()
                ->select('count(id)')
                ->from('articles as t')
                ->where($V24noxecbnll) 
                ->queryScalar();
        
        
        
        
        
        return array(
            'total_result'=>$Vfx3dng1y1f3,
            'keyword'=>$Vmq5toeun05t,
            'page_number'=>$Veggaarzpo5h_no,
            'page_size'=>$Veggaarzpo5h_size,
            'category'=>$Vceaol1c1qxm,
            'total_page'=>($Vfx3dng1y1f3 - ($Vfx3dng1y1f3 % $Veggaarzpo5h_size))/$Veggaarzpo5h_size + (($Vfx3dng1y1f3 % $Veggaarzpo5h_size ===0)?0:1),
            'data'=>$Vxun03s52uac
        );
    }
}
